package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ShoppingCarMapper;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderSubmitVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;
    /**
     * 下单业务
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result<OrderSubmitVO> submitOrder(OrdersSubmitDTO dto) {
        //1.判断收件地址是否为空
        AddressBook addressBook = addressBookMapper.getById(dto.getAddressBookId());
        if(addressBook==null){
            return Result.error(MessageConstant.ADDRESS_BOOK_IS_NULL);
        }
        //2.判断购物车是否为空
        List<ShoppingCart> cartList = shoppingCarMapper.list(BaseContext.getCurrentId());
        if(cartList==null || cartList.size()==0){
            return Result.error(MessageConstant.SHOPPING_CART_IS_NULL);
        }

        //3.插入订单数据表一条
        Orders orders=new Orders();
        BeanUtils.copyProperties(dto,orders);
        //订单号
        orders.setNumber(String.valueOf(System.currentTimeMillis()));
        //订单状态,未支付
        orders.setStatus(Orders.PENDING_PAYMENT);
        //支付状态,未支付
        orders.setPayStatus(Orders.UN_PAID);
        //下单用户id
        orders.setUserId(BaseContext.getCurrentId());
        //下单时间
        orders.setOrderTime(LocalDateTime.now());
        //手机号，地址，收货人
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());

        orderMapper.addOrder(orders);

        //4.插入订单详情表 一条或者多条
        List<OrderDetail> orderDetailList=new ArrayList<>();
        cartList.forEach(shoppingCart -> {
            OrderDetail orderDetail=new OrderDetail();
            BeanUtils.copyProperties(shoppingCart,orderDetail);
            orderDetail.setOrderId(orders.getId());
            orderDetailList.add(orderDetail);
        });
        orderDetailMapper.addOrderDetail(orderDetailList);

        //5.清空购物车数据
        shoppingCarMapper.clean(BaseContext.getCurrentId());
        //6.返回数据
        OrderSubmitVO vo = OrderSubmitVO.builder()
                .id(orders.getId())
                .orderTime(orders.getOrderTime())
                .orderNumber(orders.getNumber())
                .orderAmount(orders.getAmount())
                            .build();
        return Result.success(vo);
    }
}
